extends Control

signal startgame
signal key_strock_changed

enum Key_Strock_Type{
	None,
	Move,
	Da,
	Key_A,
	Key_B,
}

@onready var start: TextureButton = $Start
@onready var setting: TextureButton = $Setting
@onready var key_strock: Button = $Setting/SettingCan/Background/KeyStrock
@onready var save_key_strock: Button = $Setting/SettingCan/Background/KeyStrock/KeyStrockCan/SaveKeyStrock
@onready var quit_key_strock: Button = $Setting/SettingCan/Background/KeyStrock/KeyStrockCan/QuitKeyStrock
@onready var back: Button = $Setting/SettingCan/Background/Back
@onready var quit: TextureButton = $Quit

@onready var logtext: Label = $Log/Logtext

@onready var setting_can: CanvasLayer = $Setting/SettingCan
@onready var key_strock_can: CanvasLayer = $Setting/SettingCan/Background/KeyStrock/KeyStrockCan

@onready var move: TouchScreenButton = $Setting/SettingCan/Background/KeyStrock/KeyStrockCan/ColorRect/CenterMove/Move
@onready var da: TouchScreenButton = $Setting/SettingCan/Background/KeyStrock/KeyStrockCan/ColorRect/CenterDA/DA
@onready var key_a: TouchScreenButton = $Setting/SettingCan/Background/KeyStrock/KeyStrockCan/ColorRect/CenterA/Key_A
@onready var key_b: TouchScreenButton = $Setting/SettingCan/Background/KeyStrock/KeyStrockCan/ColorRect/CenterB/Key_B

@onready var center_move: Marker2D = $Setting/SettingCan/Background/KeyStrock/KeyStrockCan/ColorRect/CenterMove
@onready var center_da: Marker2D = $Setting/SettingCan/Background/KeyStrock/KeyStrockCan/ColorRect/CenterDA
@onready var center_a: Marker2D = $Setting/SettingCan/Background/KeyStrock/KeyStrockCan/ColorRect/CenterA
@onready var center_b: Marker2D = $Setting/SettingCan/Background/KeyStrock/KeyStrockCan/ColorRect/CenterB


var save_name :String = "Enter"
var key_strock_type : Key_Strock_Type = Key_Strock_Type.None 


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	init_hide()
	pass # Replace with function body.


func _input(event):
	if event is InputEventScreenTouch:
		if (move.get_global_mouse_position()-center_move.position).length()<128:
			key_strock_type = Key_Strock_Type.Move
		elif (da.get_global_mouse_position()-center_da.position).length()<128:
			key_strock_type = Key_Strock_Type.Da
		elif (key_a.get_global_mouse_position()-center_a.position).length()<128:
			key_strock_type = Key_Strock_Type.Key_A
		elif (key_b.get_global_mouse_position()-center_b.position).length()<128:
			key_strock_type = Key_Strock_Type.Key_B
		else :
			key_strock_type = Key_Strock_Type.None
		to_top(key_strock_type)
	if event is InputEventScreenDrag:
		match key_strock_type:
			Key_Strock_Type.Move: 
				center_move.position = move.get_global_mouse_position()
			Key_Strock_Type.Da: 
				center_da.position = da.get_global_mouse_position()
			Key_Strock_Type.Key_A: 
				center_a.position = key_a.get_global_mouse_position()
			Key_Strock_Type.Key_B: 
				center_b.position = key_b.get_global_mouse_position()


func init_hide():
	setting_can.hide()
	key_strock_can.hide()

func to_top(key_type : Key_Strock_Type):
	match key_type:
			Key_Strock_Type.Move: 
				center_move.z_index = 1
				center_da.z_index = 0
				center_a.z_index = 0
				center_b.z_index = 0
			Key_Strock_Type.Da: 
				center_move.z_index = 0
				center_da.z_index = 1
				center_a.z_index = 0
				center_b.z_index = 0
			Key_Strock_Type.Key_A: 
				center_move.z_index = 0
				center_da.z_index = 0
				center_a.z_index = 1
				center_b.z_index = 0
			Key_Strock_Type.Key_B: 
				center_move.z_index = 0
				center_da.z_index = 0
				center_a.z_index = 0
				center_b.z_index = 1



func _on_start_pressed() -> void:
	hide()
	# print("start_pressed")
	startgame.emit()
	pass # Replace with function body.


func _on_log_pressed() -> void:
	if logtext.visible:
		logtext.hide()
	else :
		logtext.show()
	pass # Replace with function body.



func _on_key_strock_pressed() -> void:
	key_strock_can.show()
	pass # Replace with function body.


func _on_save_key_strock_pressed() -> void:
	key_strock_changed.emit()
	# main_info.set_move_position(move.position)
	pass # Replace with function body.


func _on_quit_key_strock_pressed() -> void:
	key_strock_can.hide()
	# print("关闭按键设置")
	pass # Replace with function body.


func _on_back_pressed() -> void:
	setting_can.hide()
	pass # Replace with function body.


func _on_setting_pressed() -> void:
	setting_can.show()
	pass # Replace with function body.


func _on_quit_pressed() -> void:
	get_tree().quit()
	pass # Replace with function body.

func get_save_name()->String:
	return save_name


func save_data():
	var dict = {
		"node" : "Enter",
		"move_position_x" : center_move.position.x,
		"move_position_y" : center_move.position.y,
		"da_position_x" : center_da.position.x,
		"da_position_y" : center_da.position.y,
		"key_a_position_x" : center_a.position.x,
		"key_a_position_y" : center_a.position.y,
		"key_b_position_x" : center_b.position.x,
		"key_b_position_y" : center_b.position.y,
	}
	return dict

func load_data(data):
	center_move.position.x = data.get("move_position_x",440)
	center_move.position.y = data.get("move_position_y",768)
	center_da.position.x = data.get("da_position_x",2000)
	center_da.position.y = data.get("da_position_y",500)
	center_a.position.x = data.get("key_a_position_x",2000)
	center_a.position.y = data.get("key_a_position_y",500)
	center_b.position.x = data.get("key_b_position_x",2000)
	center_b.position.y = data.get("key_b_position_y",500)
